/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/
package servlets;

import utils.Utilizzatore;
import utils.DbManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alice
*/
@WebServlet(name="Autenticazione", urlPatterns={"/Autenticazione","/login","/error","/errore_registraz","/registrato"})
public class Autenticazione extends HttpServlet {
    private DbManager manager;
   
    @Override
public void init() throws ServletException {
// inizializza il DBManager dagli attributi di Application
this.manager = (DbManager)super.getServletContext().getAttribute("dbmanager");
}

  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      
     String context=request.getRequestURI();
     if(context.equals("/PROGETTO_WEB_2/Autenticazione")){
       // response.sendRedirect("login.jsp");
       //  HttpSession session= request.getSession();
     
 
 String username= request.getParameter("username");
 String password= request.getParameter("password");
 String rete=request.getParameter("optionsRadios");
 System.out.println("USEN"+username);
 System.out.println("RETE"+rete);
 
 Utilizzatore user = null;


 try{
     user=manager.authenticate(username, password);

     } catch (SQLException ex) {
throw new ServletException(ex);
} 


if (user == null) {
    String message="Username o Password Errati !";
    response.sendRedirect("error.jsp?message="+URLEncoder.encode(message, "UTF-8"));
} else {
HttpSession session = request.getSession(true);
System.out.println("UN"+user.getUsername());
user.setRete(rete);
session.setAttribute("user", user);
session.setAttribute("username", username);

String roles=user.getRoles();
String nome=user.getNome();
System.out.println("NOME"+nome);

if(roles.equals("utilizzatore")){
    user.setRete(rete);
    session.setAttribute("utilizzatore",user);
    String value= user.getNome()+" "+user.getCognome();
    response.sendRedirect(request.getContextPath() +"/ControlloScadenzaProdotti");
     System.out.println("CP= "+request.getContextPath());}
    //response.sendRedirect("landing_utilizzatore.jsp?user="+URLEncoder.encode(value, "UTF-8"));}
else if (roles.equals("amministratore")){
        session.setAttribute("amministratore",user);
        String value= user.getNome()+" "+user.getCognome();
    //    response.sendRedirect("landing_amministratore.jsp?user="+value);
        response.sendRedirect(request.getContextPath()+ "/AmministratoreAsteCorrenti");
    }
        
}

     }else{
HttpSession session = request.getSession();
//User user = (User) session.getAttribute("user");
//session.invalidate();
response.sendRedirect("login.jsp");
}
 
 }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}



